# @Author ZhangGJ
# @Date 2021/12/01 11:30

import cv2.cv2 as cv2
import numpy as np


# 绘制几何图像的轮廓
def draw_contour():
    img = cv2.imread('../images/shape1.png')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(img, contours, -1, (0, 0, 255), 5)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_contour2():
    img = cv2.imread('../images/flower.jpg')
    cv2.imshow('img', img)
    img = cv2.medianBlur(img, 5)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    cv2.imshow('binary', binary)
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
    cv2.imshow('contours', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 轮廓拟合 矩形包围框
def contour_fit():
    img = cv2.imread('../images/shape2.png')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    x, y, w, h = cv2.boundingRect(contours[0])
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 轮廓拟合 圆形包围框
def contour_fit2():
    img = cv2.imread('../images/shape2.png')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    t, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    center, radius = cv2.minEnclosingCircle(contours[0])
    x = int(round(center[0]))
    y = int(round(center[1]))
    cv2.circle(img, (x, y), int(radius), (0, 0, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 凸包
def convex_hull():
    img = cv2.imread('../images/shape2.png')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    hull = cv2.convexHull(contours[0])
    cv2.polylines(img, [hull], True, (0, 0, 255), 2)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# canny边缘检测
def canny():
    img = cv2.imread('../images/flower.jpg')
    r1 = cv2.Canny(img, 10, 50)
    r2 = cv2.Canny(img, 100, 200)
    r3 = cv2.Canny(img, 400, 600)
    cv2.imshow('img', img)
    cv2.imshow('r1', r1)
    cv2.imshow('r2', r2)
    cv2.imshow('r3', r3)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 霍夫变换
def hough_line():
    img = cv2.imread('../images/pen.jpg')
    o = img.copy()
    o = cv2.medianBlur(o, 5)
    gray = cv2.cvtColor(o, cv2.COLOR_BGR2GRAY)
    binary = cv2.Canny(o, 50, 150)
    lines = cv2.HoughLinesP(binary, 1, np.pi / 180, 15, minLineLength=100, maxLineGap=18)
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
    cv2.imshow('canny', binary)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 圆形检测
def hough_circle():
    img = cv2.imread('../images/coin.jpg')
    o = img.copy()
    o = cv2.medianBlur(o, 5)
    gray = cv2.cvtColor(o, cv2.COLOR_BGR2GRAY)
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 70, param1=100, param2=25, minRadius=10,
                               maxRadius=50)
    circles = np.uint(np.around(circles))
    for c in circles[0]:
        x, y, r = c
        cv2.circle(img, (x, y), r, (0, 0, 255), 3)
        cv2.circle(img, (x, y), 2, (0, 0, 255), 3)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    hough_circle()
